//导入vue构造函数
import Vue from "vue";
// 导入router构造函数
import Router from "vue-router";

// 导入8个动态路由对象
import approalsRouter from "./modules/approvals";
import attendancesRouter from "./modules/attendances";
import departmentsRouter from "./modules/departments";
import employeesRouter from "./modules/employees";
import permissionsRouter from "./modules/permissions";
import salarysRouter from "./modules/salarys";
import settingsRouter from "./modules/settings";
import social_securitys from "./modules/social_securitys";

// 动态路由表数组
export const asyncRoutes = [
  approalsRouter,
  settingsRouter,
  employeesRouter,
  attendancesRouter,
  departmentsRouter,
  permissionsRouter,
  salarysRouter,
  social_securitys,
];
//静态路由表（所有人都可以访问的路由）
export const constantRoutes = [
  {
    //登入
    path: "/login",
    //懒加载
    component: () => import("@/views/login/index"),
    // hidden：true 表示当前路又不会出现在主页的左侧菜单
    hidden: true,
  },

  {
    //404
    path: "/404",
    component: () => import("@/views/404"),
    hidden: true,
  },

  {
    //Layout
    path: "/",
    component: Layout,
    //重定向
    redirect: "/dashboard",
    // hidden: true,可以加在外面   也可以加在内部 都会影隐藏
    // title:菜单名称
    //icon：菜单小图标
    children: [
      //首页
      {
        path: "dashboard",
        name: "dashboard",
        //name属性必须添加  以后我们需要基于name属性去userInfo.roles.menus中对比
        //name必须与userInfo保持一致
        component: () => import("@/views/dashboard/index"),
        meta: { title: "首页", icon: "dashboard" },
      },
    ],
  },

  // 404 page must be placed at the end !!!
];

Vue.use(Router);

/* Layout */
import Layout from "@/layout";

// todo:动态路由表（需要通过权限控制路由）

// 创建路由实例  吧new VueRouter（）写在了函数中
const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    // 路由模式  默认是hash模式  可以选择history历史模式
    // mode：‘history’
    // 当路由切换的时候 让页面回到最顶部
    scrollBehavior: () => ({ y: 0 }),
    // 临时全部合并
    // routes: [...constantRoutes, ...asyncRoutes],
    routes: constantRoutes,
  });
// 调用创建路由函数实例  接收路由实例
const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
// 重置路由的方法
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

// 导出router实例
export default router;
